home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
oper_sys
/
quartz
/
quartz10.lha
/
Pexample
/
StopWatch.h
< prev
Wrap
C/C++ Source or Header
|
1990-05-14
|
3KB
|
129 lines
//
// StopWatch - allows timing of code sections. Current implementation
// supports timing of up to an unsigned long worth of microseconds, or
// about 71.5 minutes (2^32-1 = 4,294,nnn,nnn usecs = 4,294 secs = 71.5 mins).
//
// Author: John Faust
// Date: July 17, 1989.
//
// Modification History:
//
// Oct-10-1989 JEF
// Merged two similar files, yielding one StopWatch class with
// a single interface which utilizes either standard Unix functions
// (gettimeofday) or the sequent usec timer.
//
#ifndef StopWatch_h
#define StopWatch_h
//
// The following #define was added to allow testing of both code paths
// when on the Sequent.
//
#ifdef sequent
#define NonPortableSW
#endif sequent
#ifdef NonPortableSW
#include <usclkc.h>
#else
#include <sys/time.h>
#include <sys/resource.h>
extern int gettimeofday (struct timeval*, struct timezone*);
#endif NonPortableSW
#include <stream.h>
class STOPWATCH
{
#ifdef NonPortableSW
static shared_t usclk_t StartTime1;
#else
struct timeval StartTime;
#endif NonPortableSW
public:
//
// Constructor routine.
//
STOPWATCH ()
{
#ifdef NonPortableSW
usclk_init ();
#else
;
#endif NonPortableSW
};
//
// Destructor routine.
//
~STOPWATCH ()
{ ; };
//
// Start - start stopwatch.
//
void
Start ()
{
#ifdef NonPortableSW
StartTime1 = getusclk ();
#else
struct timezone zone;
gettimeofday (&StartTime, &zone);
#endif NonPortableSW
};
//
// Mark - return time in usecs since Start.
//
unsigned long
Mark ()
{
#ifdef NonPortableSW
usclk_t EndTime = getusclk ();
usclk_t Usecs;
//
// The sequent usec timer can wrap. Check if wrap occurred and
// recover.
//
if (EndTime > StartTime1)
Usecs = EndTime - StartTime1;
else
{
cout << form ("usec clock wrapped!\n");
Usecs = (0xffffffff - StartTime1) + EndTime;
}
// cout << form ("Usecs = %u\n", Usecs); cout.flush ();
return ( (unsigned long) Usecs );
#else
struct timeval EndTime;
struct timezone zone;
unsigned long usecs;
gettimeofday (&EndTime, &zone);
usecs = ((EndTime.tv_sec - StartTime.tv_sec) * 1000000) +
(EndTime.tv_usec - StartTime.tv_usec);
// cout << form ("Es=%u, Ss=%u, Eu=%u, Su=%u, R=%u\n",
// EndTime.tv_sec,
// StartTime.tv_sec,
// EndTime.tv_usec,
// StartTime.tv_usec,
// usecs);
// cout.flush ();
return usecs;
#endif NonPortableSW
};
};
#endif StopWatch_h